import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from "@/views/LoginView.vue";
import userView from "@/views/userView.vue";
import LayoutView from "@/views/LayoutView.vue";
import RegisterView from "@/views/RegisterView.vue";
import PersonView from "@/views/PersonView";
import familyMemberView from "@/views/familyMemberView.vue";
import HealthView from "@/views/HealthView.vue";
import CareView from "@/views/CareView.vue";
import store from '@/store'
import AdminRegisterView from "@/views/AdminRegisterView.vue";
import WheelchairView from "@/views/WheelchairView";
import MessageView from "@/views/MessageView.vue";
import AllInvitationRecordsView from "@/views/AllInvitationRecordsView.vue";
import AllInvitedRecordsView from "@/views/AllInvitedRecordsView.vue";
import SleepView from "@/views/SleepView.vue";

Vue.use(VueRouter)

const curRandomPathKey = localStorage.getItem('randomPathKey') || '1qaz2wsx3e';

const routes = [
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView
  },
  {
    path: `/register/admin/${curRandomPathKey}`,
    name: 'adminRegister',
    component: AdminRegisterView
  },
  {
    path: '/',
    name: 'Layout',
    component: LayoutView,
    children: [
      {
        path: '',
        name: 'home',
        component: HomeView
      },
      {
        path: 'user',
        name: 'user',
        component: userView
      },
      {
        path: 'person',
        name: 'person',
        component: PersonView
      },
      {
        path: '/familyMember',
        name: 'familyMember',
        component: familyMemberView
      },
      {
        path: '/health',
        name: 'health',
        component: HealthView
      },
      {
        path: '/sleep',
        name: 'sleep',
        component: SleepView
      },
      {
        path: '/care',
        name: 'care',
        component: CareView
      },
      {
        path: '/wheelchair',
        name: 'wheelchair',
        component: WheelchairView
      },
      {
        path: '/message',
        name: 'message',
        component: MessageView,
        children: [
          {
            path: '/allInvitationRecords',
            name: 'allInvitationRecords',
            component: AllInvitationRecordsView
          },
          {
            path: '/allInvitedRecords',
            name: 'allInvitedRecords',
            component: AllInvitedRecordsView
          },
          // {
          //   path: '/chat',
          //   name: 'chat',
          //   component: ChatView
          // },
          // {
          //   path: '/orderRecords',
          //   name: 'orderRecords',
          //   component: OrderRecordsView
          // }
        ]
      }
    ]
  },
  {
    path: '/404',
    name: '404',
    component: () => import('../views/404View.vue')
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//  提供一个重置路由的方法
export const resetRouter = () => {
  router.matcher = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
  })
}

//  路由守卫
router.beforeEach((to, from, next) => {
  // localStorage.setItem("currentPathName", to.name)  //设置当前的路由名称，为了在Header组件中去使用
  store.commit("setPath") //  触发store的数据更新

  //  未找到路由的情况
  if(!to.matched.length) {
    const storeMenu = localStorage.getItem("menus")
    if(storeMenu) {
      // if(true) {
      next("/")
    } else {
      //  跳回登陆界面
      next("/404")
    }
  } else {
    //  其他的情况都放行
    next()
  }
})

export default router
